<?php

namespace app\client\controller\subject;

use app\common\controller\Frontend;
use app\common\enums\LearnMeansEnum;
use app\common\model\banxi\subject\SubjectQuestion;
use app\common\model\banxi\learn\LearnSkRecord;
use app\common\service\learn\LearnSkRecordService;
use app\common\model\banxi\learn\LearnSubjectQuestionRecord;
use app\common\model\banxi\learn\LearnSubjectQuestionAnswerRecord;


class Question extends Frontend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
    }


    /*
     * 开始答题
     * */
    public function startQuestion()
    {
        $skRecordId = $this->request->post('skRecordId');
        $exerciseType = $this->request->post('exerciseType');
        $subjectTeachId = $this->request->post('subjectTeachId',0);
        $subjectTeachChapterId = $this->request->post('subjectTeachChapterId',0);

        if (!in_array($exerciseType,[1,2,3])){
            $this->fail('请选择做题类型');
        }
        $trainerId = null;
        $studentId = $this->auth->id;
        $learnMeans = LearnMeansEnum::E2; // 默认为练习模式（对应 LearnMeans.E2）

        // 如果提供了上课记录ID
        if ($skRecordId) {
            // 查询上课记录
            $learnSkRecord = LearnSkRecord::where('id', $skRecordId)->find();

            // 验证上课记录是否存在
            if (empty($learnSkRecord)) {
                $this->fail('上课记录不存在');
            }

//            // 验证教练ID是否匹配
//            if ($learnSkRecord['trainerId'] != $trainerId) {
//                $this->fail('上课记录不存在');
//            }

            // 获取学生ID
            $studentId = $learnSkRecord['studentId'];
            $learnMeans = LearnMeansEnum::E1; // 设置为教学模式（对应 LearnMeans.E1）
        }
        $skRecordId = $skRecordId ? $skRecordId : null;

        $LearnSkRecordService = new LearnSkRecordService();
        $result = $LearnSkRecordService->startSubjectQuestion($skRecordId, $studentId, $trainerId, $learnMeans,$exerciseType,$subjectTeachId,$subjectTeachChapterId);

        if (!$result){
            $this->fail($LearnSkRecordService->getError());
        }
        $this->ok('提交成功', $result);
    }

    /*
     * 答案提交
     * */
    public function answerQuestion()
    {
        $recordId = $this->request->post('recordId');
        $answerList = $this->request->post('answerList');

        if (!$recordId){
            $this->fail('记录id不能为空');
        }
        if (!$answerList){
            $this->fail('提交答案不能为空');
        }
        $studentId = $this->auth->id;

        $LearnSkRecordService = new LearnSkRecordService();
        $result = $LearnSkRecordService->answerSubjectQuestion($recordId, null, $studentId, $answerList);
        if (!$result){
            $this->fail($LearnSkRecordService->getError());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    /*
     * 获取答题记录
     * */
    public function getAnswerDetail()
    {
        $recordId = $this->request->get('recordId',0);

        $LearnSubjectQuestionRecordModel = new LearnSubjectQuestionRecord();
        $recordInfo = $LearnSubjectQuestionRecordModel->where('id', $recordId)
            ->field('id,skRecordId,subjectTeachName,subjectTeachChapterName,trainerId,studentId,exerciseType,rightNum,errorNum,rightRate,isSubmit,totalNum,answerStartTime,answerEndTime,useTime,comment')
            ->find();
        if (!$recordInfo){
            $this->fail('记录不存在');
        }
        if ($recordInfo['isSubmit'] != 1){
            $this->fail('答题未提交');
        }
        //上课记录
        $LearnSkRecordModel = new LearnSkRecord();
        $skRecordInfo = $LearnSkRecordModel->where('id', $recordInfo['skRecordId'])->field('id,courseName,actualCourseId,courseFullType,trainerStar,trainerEvaluate,afterClassQuestion')->find();
        $LearnSubjectQuestionAnswerRecordModel = new LearnSubjectQuestionAnswerRecord();
        $answerList = $LearnSubjectQuestionAnswerRecordModel->where('recordId', $recordId)->select();

        $this->ok('操作成功', ['recordInfo' => $recordInfo,'skRecordInfo' => $skRecordInfo, 'answerList' => $answerList]);
    }
}
